# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: net_util.py
# Time: 16:13

import json
import requests


def http_requests(url, data=None, method="POST", files=None, **headers):
    '''
    发起HTTP请求信息
    :param url: 请求地址
    :param data: 请求参数，以字典格式传输,如：data = {"name":"xiaoxin"}
    :param method: 请求方式，默认POST
    :param files: 上传文件，如：files={"file":open("2020-08-03_11.xls","rb")}
    :param headers: 头部信息，可选参数
    :return:返回请求结果，以json格式返回
    '''
    headers = headers.get("headers")
    try:
        if method == "POST":
            response = requests.post(url, data=data, files=files, headers=headers)
            response.encoding = "utf-8"
            return response.json()
        elif method == "GET":
            response = requests.get(url, params=data, headers=headers)
            response.encoding = "utf-8"
            return response.text
        else:
            print("选择请求方式错误，请进一步添加接口请求方式！")
    except Exception as e:
        return "请求出错了{}".format(e)


